package com.jxs.android.weixi;

import java.util.ArrayList;
import java.util.List;

import org.ksoap2.serialization.SoapObject;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jxs.android.weixi.data.WrkInfo;
import com.telecom.weixi.R;

public class MyWork extends MyCommnList {

	private List<WrkInfo> resInfos = new ArrayList<WrkInfo>();
	private WrkInfoAdapter adapter;

	@Override
	public String getMethod() {
		return "getWorkStat";
	}

	@Override
	public Object paserData(SoapObject retMsgObject) {
		resInfos.clear();
		/**
		 * @id 0
		 * @serv_exe_num:用户维系-已执行
		 * @serv_unexe_num:用户维系-需维系
		 * @serv_exec_rate:用户维系-完成率
		 * @serv_flag:标示是否显示用户维系（0不显示，1显示）
		 */
		WrkInfo wrkInfo = new WrkInfo();
		wrkInfo.id = 0;
		wrkInfo.title = "用户维系";
		wrkInfo.name_1 = "已执行";
		wrkInfo.name_1_value = retMsgObject.getPropertySafelyAsString("serv_exe_num");
		wrkInfo.name_2 = "需维系";
		wrkInfo.name_2_value = retMsgObject.getPropertySafelyAsString("serv_unexe_num");
		wrkInfo.name_3 = "完成率";
		wrkInfo.name_3_value = retMsgObject.getPropertySafelyAsString("serv_exec_rate");
		wrkInfo.flag = "1".equals(retMsgObject.getPropertySafelyAsString("serv_flag"));
		if (wrkInfo.flag)
			resInfos.add(wrkInfo);

		/**
		 * @id 1
		 * @zq_exe_num:政企维系-已执行
		 * @zq_unexe_num:政企维系-需维系
		 * @zq_exec_rate:政企维系-完成率
		 * @zq_flag:标示是否显示政企维系（0不显示，1显示）
		 */
		wrkInfo = new WrkInfo();
		wrkInfo.id = 1;
		wrkInfo.title = "政企维系";
		wrkInfo.name_1 = "已执行";
		wrkInfo.name_1_value = retMsgObject.getPropertySafelyAsString("zq_exe_num");
		wrkInfo.name_2 = "需维系";
		wrkInfo.name_2_value = retMsgObject.getPropertySafelyAsString("zq_unexe_num");
		wrkInfo.name_3 = "完成率";
		wrkInfo.name_3_value = retMsgObject.getPropertySafelyAsString("zq_exec_rate");
		wrkInfo.flag = "1".equals(retMsgObject.getPropertySafelyAsString("zq_flag"));
		if (wrkInfo.flag)
			resInfos.add(wrkInfo);
		/**
		 * @id 2
		 * @vip_exe_num:VIP用户维系-已执行
		 * @vip_unexe_num:VIP用户维系-需维系
		 * @vip_exec_rate:VIP用户维系-完成率
		 * @vip_flag:标示是否显示VIP用户维系（0不显示，1显示）
		 */
		wrkInfo = new WrkInfo();
		wrkInfo.id = 2;
		wrkInfo.title = "VIP用户维系";
		wrkInfo.name_1 = "已执行";
		wrkInfo.name_1_value = retMsgObject.getPropertySafelyAsString("vip_exe_num");
		wrkInfo.name_2 = "需维系";
		wrkInfo.name_2_value = retMsgObject.getPropertySafelyAsString("vip_unexe_num");
		wrkInfo.name_3 = "完成率";
		wrkInfo.name_3_value = retMsgObject.getPropertySafelyAsString("vip_exec_rate");
		wrkInfo.flag = "1".equals(retMsgObject.getPropertySafelyAsString("vip_flag"));
		if (wrkInfo.flag)
			resInfos.add(wrkInfo);
		/**
		 * @id 3
		 * @vpn_alert_num:VPN维系-需维系
		 * @vpn_exec_num:VPN维系-执行数
		 * @vpn_exec_rate:VPN维系-执行率
		 * @vpn_flag:标示是否显示VIP用户维系（0不显示，1显示）
		 */
		wrkInfo = new WrkInfo();
		wrkInfo.id = 3;
		wrkInfo.title = "VPN维系";
		wrkInfo.name_1 = "需维系";
		wrkInfo.name_1_value = retMsgObject.getPropertySafelyAsString("vpn_alert_num");
		wrkInfo.name_2 = "执行数";
		wrkInfo.name_2_value = retMsgObject.getPropertySafelyAsString("vpn_exec_num");
		wrkInfo.name_3 = "执行率";
		wrkInfo.name_3_value = retMsgObject.getPropertySafelyAsString("vpn_exec_rate");
		wrkInfo.flag = "1".equals(retMsgObject.getPropertySafelyAsString("vpn_flag"));
		if (wrkInfo.flag)
			resInfos.add(wrkInfo);
		/**
		 * @id 4
		 * @send_user_num:我的派单-用户数
		 * @send_exec_num:我的派单-已执行
		 * @send_exec_rate:我的派单-完成率
		 * @send_flag:标示是否我的派单（0不显示，1显示）
		 */
		wrkInfo = new WrkInfo();
		wrkInfo.id = 4;
		wrkInfo.title = "我的派单";
		wrkInfo.name_1 = "用户数";
		wrkInfo.name_1_value = retMsgObject.getPropertySafelyAsString("send_user_num");
		wrkInfo.name_2 = "已执行";
		wrkInfo.name_2_value = retMsgObject.getPropertySafelyAsString("send_exec_num");
		wrkInfo.name_3 = "完成率";
		wrkInfo.name_3_value = retMsgObject.getPropertySafelyAsString("send_exec_rate");
		wrkInfo.flag = "1".equals(retMsgObject.getPropertySafelyAsString("send_flag"));
		if (wrkInfo.flag)
			resInfos.add(wrkInfo);

		return resInfos;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void updateList(Object msgObj) {
		adapter.setWrkInfos((List<WrkInfo>) msgObj);
		adapter.notifyDataSetChanged();
	}

	@Override
	public OnItemClickListener getItemClickListener() {
		return new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int location, long arg3) {
				Intent intent = new Intent();
				Bundle bundle = new Bundle();
				WrkInfo info = resInfos.get(location - 1);
				if (info.id == 0) {
					intent.setClass(MyWork.this, MyWork_UserDetail_Laucher.class);
				} else if (info.id == 1) {
					intent.setClass(MyWork.this, MyWork_Customer.class);
				} else if (info.id == 2) {
					intent.setClass(MyWork.this, MyWork_VIP_Laucher.class);
				} else if (info.id == 3) {
					intent.setClass(MyWork.this, MyWork_VPN.class);
				} else {
					intent.setClass(MyWork.this, MyDoing.class);
				}
				bundle.putString("title", info.title);
				intent.putExtras(bundle);
				startActivity(intent);
			}
		};
	}

	@Override
	public BaseAdapter getAdapter(Context context) {
		adapter = new WrkInfoAdapter(context);
		return adapter;
	}

	@Override
	public String getTopBarTitle() {
		return getString(R.string.home_work);
	}

	class WrkInfoAdapter extends BaseAdapter {

		List<WrkInfo> wrkInfos = new ArrayList<WrkInfo>();

		private LayoutInflater inflater = null;
		private ViewHolder holder = null;

		public WrkInfoAdapter(Context context) {
			inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		public void setWrkInfos(List<WrkInfo> custInfos) {
			this.wrkInfos.clear();
			this.wrkInfos.addAll(custInfos);
		}

		@Override
		public int getCount() {
			return wrkInfos.size();
		}

		@Override
		public WrkInfo getItem(int position) {
			return wrkInfos.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			WrkInfo custInfo = wrkInfos.get(position);

			if (convertView == null) {
				convertView = inflater.inflate(R.layout.bu_list_item, null);
				holder = new ViewHolder();
				holder.icon_iv = (ImageView) convertView.findViewById(R.id.item_icon);
				holder.title_tv = (TextView) convertView.findViewById(R.id.item_title);
				holder.value_tv = (TextView) convertView.findViewById(R.id.item_des);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			/*
			 * serv_gz_num:公众类型用户数 serv_zq_num:政企类型用户数 serv_all_num:用户数
			 * cust_gz_num:公众类型客户数 cust_zq_num:政企类型客户数 cust_all_num:客户数
			 */
			holder.title_tv.setText(custInfo.title);
			holder.title_tv.setTextColor(Color.rgb(0, 0, 0));
			holder.value_tv.setText(custInfo.name_3 + ":" + custInfo.name_3_value + "  " + custInfo.name_2 + ":" + custInfo.name_2_value + custInfo.name_1 + ":"
			        + custInfo.name_1_value);
			return convertView;
		}

		class ViewHolder {
			ImageView icon_iv;
			TextView title_tv;
			TextView value_tv;
		}

	}

}
